//1.定义路由表
import Home from "../views/Home.vue";
import Hello from "../views/Hello.vue";
import {createRouter, createWebHashHistory} from "vue-router"

const routes = [
    {path: '/', component: Home},
    {path: '/hello', name: 'Hello', component: Hello},
    {
        path: '/haha/:id/:name/:age',
        name: 'Haha',
        component: () => import('../views/Haha.vue')
    },
    {
        path: '/user/:id',
        component: () => import('../views/user/User.vue'),
        name: 'User',
        children: [
            {
                path: 'profile',
                name: 'profile',
                component: () => import('../views/user/userProfile.vue'),
            },
            {
                path: 'posts',
                name: 'posts',
                component: () => import('../views/user/userPosts.vue'),
            }
        ]
    }
];
//2.创建路由器
let router = createRouter({
    history: createWebHashHistory(),
    routes,
});

router.beforeEach((to, from) => {
    console.log("to",to);
    console.log("from", from);
    //1.返回false，取消导航
    //2.返回true，不返回，继续导航
    //3.返回'路径'，跳转到指定页面
    if (to.path==='/hello'){
        console.log("禁止访问");
        return '/';
    }
})

//3.导出路由器
export default router;
//4.vue实例使用路由器
